/**
 * 
 */
var ylChart1 = null;;
var ylChart2 = null;
var ylChart3 = null;
$('document').ready(function(){
	
	initYingliSelector();
	initJiasuduSelector();
	
	ylChart1 = echarts.init(document.getElementById('ylChart1'));
	ylChart2 = echarts.init(document.getElementById('ylChart2'));
	ylChart3 = echarts.init(document.getElementById('ylChart3'));
	
	jsdChart1 = echarts.init(document.getElementById('jsdChart1'));
	jsdChart2 = echarts.init(document.getElementById('jsdChart2'));
	jsdChart3 = echarts.init(document.getElementById('jsdChart3'));

});

function drawYingLi(currentTime) {
	
	let startTime = new Date(currentTime);
	let yl11 = $('#yl1selector1').val();
	let yl12 = $('#yl1selector2').val();
	let yl21 = $('#yl2selector1').val();
	let yl22 = $('#yl2selector2').val();
	let yl31 = $('#yl3selector1').val();
	let yl32 = $('#yl3selector2').val();
	
	
	// get control data
	$.ajax({
		url: 'model/strControl.do',
		type: 'get',
		data: {
			current: startTime
		}

	}).done(function(data, status, xhr){
		
		let controlStartTime = null;
		let controlEndTime = null;
		let controlId = null;
		if (data != null) {
			controlStartTime = new Date(data.strainControlStime);
			controlEndTime = new Date(data.strainControlEtime);
			controlId = data.strainControlId;
		}

		let intervalYl = setInterval(function(){

			// get real data to draw chart
			$.ajax({
				url: 'model/strDataMore.do',
				type: 'post',
				data: {
					start: startTime,
					interval: HANGYUN.REFRESH_INTERVAL,
					controlId: controlId,
					tran11: yl11,
					tran12: yl12,
					tran21: yl21,
					tran22: yl22,
					tran31: yl31,
					tran32: yl32
				}
			}).done((data, status, xhr)=>{
				debugger;
				if (data != null) {
					optionYl1.series[0].data = data.first[0];
					optionYl1.series[1].data = data.first[1];
					ylChart1.setOption(optionYl1);
					
					optionYl2.series[0].data = data.second[0];
					optionYl2.series[1].data = data.second[1];
					ylChart2.setOption(optionYl2);
					
					optionYl3.series[0].data = data.third[0];
					optionYl3.series[1].data = data.third[1];
					ylChart3.setOption(optionYl3);
	
					startTime.setSeconds(startTime.getSeconds() + HANGYUN.REFRESH_INTERVAL);
					console.log(startTime);
				} else {
					clearInterval(intervalYl);
				}
				
				if (startTime > controlEndTime) {
					clearInterval(intervalYl);
				}
			});

		}, HANGYUN.REFRESH_INTERVAL*1000);	
	});
}

/**
 * 
 * @param currentTime
 * @returns
 */
function drawJiasudu(currentTime) {
	
	let startTime = new Date(currentTime);
	var jsd1 = $('#jsdSelector1').val();
	var jsd2 = $('#jsdSelector2').val();
	var jsd3 = $('#jsdSelector3').val();
	var xyz1 = $('#xyz1').val();
	var xyz2 = $('#xyz2').val();
	var xyz3 = $('#xyz3').val();

	// get control data
	$.ajax({
		url: 'model/accControl.do',
		type: 'get',
		data: {
			current: startTime
		}

	}).done(function(data, status, xhr){
		let controlStartTime = null;
		let controlEndTime = null;
		let controlId = null;
		if (data != null) {
			controlStartTime = new Date(data.accelrationControlStime);
			controlEndTime = new Date(data.accelrationControlEtime);
			controlId = data.accelrationControlId;
		}

		let intervalJsd = setInterval(function(){

			// get real data to draw chart
			$.ajax({
				url: 'model/accDataMore.do',
				type: 'post',
				data: {
					start: startTime,
					interval: HANGYUN.REFRESH_INTERVAL,
					controlId: controlId,
					tran1: jsd1,
					tran2: jsd2,
					tran3: jsd3,
					direction1: xyz1,
					direction2: xyz2,
					direction3: xyz3
				}
			}).done((data, status, xhr)=>{

				if (data != null) {
					debugger;
					console.log(data);
					optionJsd1.series[0].data = data[jsd1];
					jsdChart1.setOption(optionJsd1);
					optionJsd2.series[0].data = data[jsd2];
					jsdChart2.setOption(optionJsd2);
					optionJsd3.series[0].data = data[jsd3];
					jsdChart3.setOption(optionJsd3);
	
					startTime.setSeconds(startTime.getSeconds() + HANGYUN.REFRESH_INTERVAL);
					console.log(startTime);
				} else {
					clearInterval(intervalJsd);
				}
				
				if (startTime > controlEndTime) {
					clearInterval(intervalJsd);
				}
				
			});

		}, HANGYUN.REFRESH_INTERVAL*1000);
	});
}

/**
 * click button 
 */
$('#submit').click(function(){

	var currentTime = new Date();
	console.log(currentTime.getTime());
	if (HANGYUN.DEMO){
		drawYingliDemo(currentTime);
		drawJiasuduDemo(currentTime);
	} else {
		drawYingLi(currentTime);
		drawJiasudu(currentTime);
	}


});

/**
 * 
 * @returns
 */
function initYingliSelector(){
	
	$.ajax({
		url: 'model/strTrans.do',
        method : "get",
	}).done(function(data, status, xhr){
		for (let i = 0; i < data.length; i++){
			$('#yl1selector1').append(
			'<option value='+data[i].transducerNumber+'>'+data[i].transducerNumber+'</option>');
			$('#yl1selector2').append(
			'<option value='+data[i].transducerNumber+'>'+data[i].transducerNumber+'</option>');
			$('#yl2selector1').append(
			'<option value='+data[i].transducerNumber+'>'+data[i].transducerNumber+'</option>');
			$('#yl2selector2').append(
			'<option value='+data[i].transducerNumber+'>'+data[i].transducerNumber+'</option>');
			$('#yl3selector1').append(
			'<option value='+data[i].transducerNumber+'>'+data[i].transducerNumber+'</option>');
			$('#yl3selector2').append(
			'<option value='+data[i].transducerNumber+'>'+data[i].transducerNumber+'</option>');
		}
		
		$("#yl1selector1").combobox({});
		$("#yl1selector2").combobox({});
		$("#yl2selector1").combobox({});
		$("#yl2selector2").combobox({});
		$("#yl3selector1").combobox({});
		$("#yl3selector2").combobox({});
	});
}

/**
 * 
 * @returns
 */
function initJiasuduSelector(){
	
	$.ajax({
		url: 'model/accTrans.do',
        method : "get",
	}).done(function(data, status, xhr){
		debugger;
		for (let i = 0; i < data.length; i++){
			$('#jsdSelector1').append(
			'<option value='+data[i].transducerNumber+'>'+data[i].transducerNumber+'</option>');
			$('#jsdSelector2').append(
			'<option value='+data[i].transducerNumber+'>'+data[i].transducerNumber+'</option>');
			$('#jsdSelector3').append(
			'<option value='+data[i].transducerNumber+'>'+data[i].transducerNumber+'</option>');
		}
		$("#jsdSelector1").combobox({});
		$("#jsdSelector2").combobox({});
		$("#jsdSelector3").combobox({});
	});
}

/**
 * demo when no data
 * @param currentTime
 * @returns
 */
function drawYingliDemo(currentTime) {
	
	let startTime = new Date(currentTime);
	let endTime = new Date(startTime.getTime()+HANGYUN.DEMO_DURATION*1000*60);
	
	let inter = setInterval(()=>{
		let valueArray = [];
		let num = 0;
		for (let i = 0; i < 100; i++) {
			num = Math.random()*(5-3) + 5;
			valueArray.push(num);
		}
		
		optionYl1.series[0].data = valueArray;
		ylChart1.setOption(optionYl1);
		
		optionYl2.series[0].data = valueArray;
		ylChart2.setOption(optionYl2);
		
		optionYl3.series[0].data = valueArray;
		ylChart3.setOption(optionYl3);
		
		startTime.setSeconds(startTime.getSeconds() + HANGYUN.REFRESH_INTERVAL);
		
		if (startTime > endTime) {
			clearInterval(inter);
		}
		
	}, HANGYUN.REFRESH_INTERVAL*1000);
}

/**
 * demo when no data
 * @param currentTime
 * @returns
 */
function drawJiasuduDemo(currentTime) {
	
	let startTime = new Date(currentTime);
	let endTime = new Date(startTime.getTime()+HANGYUN.DEMO_DURATION*1000*60);
	
	let inter = setInterval(()=>{
		let valueArray1 = [];
		let valueArray2 = [];
		let valueArray3 = [];
		let num1 = 0;
		let num2 = 0;
		let num3 = 0;
		for (let i = 0; i < 100; i++) {
			num1 = Math.random()*(5-3) + 3;
			num2 = Math.random()*(5-3) + 3;
			num3 = Math.random()*(5-3) + 3;
			valueArray1.push(num1);
			valueArray2.push(num2);
			valueArray3.push(num3);
		}
		
		optionJsd1.series[0].data = valueArray1;
		jsdChart1.setOption(optionJsd1);
		optionJsd2.series[0].data = valueArray2;
		jsdChart2.setOption(optionJsd2);
		optionJsd3.series[0].data = valueArray3;
		jsdChart3.setOption(optionJsd3);
		
		startTime.setSeconds(startTime.getSeconds() + HANGYUN.REFRESH_INTERVAL);
		
		if (startTime > endTime) {
			clearInterval(inter);
		}
		
	}, HANGYUN.REFRESH_INTERVAL*1000);
}

